﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC.LevelIndependentTag
{
	public class MarkPipeLevelUtility
	{
		public static List<Line> DrawTriangle(Autodesk.Revit.DB.View view, XYZ pipePoint, XYZ location, ref XYZ tagLocation, ref DirectionVector directionVector, bool hasLeadWire, YJKLine hyline, double movingDistance)
		{
			List<Line> list = new List<Line>();
			YJKLine hyline2 = new YJKLine(new XYZ(hyline.StartPoint.X, hyline.StartPoint.Y, 0.0), new XYZ(hyline.EndPoint.X, hyline.EndPoint.Y, 0.0));
			hyline2.MakeUnBound();
			YJKLine verticalHyLine = MarkPipeLevelUtility.GetVerticalHyLine(hyline2, new XYZ(pipePoint.X, pipePoint.Y, 0.0));
			verticalHyLine.MakeUnBound();
			XYZ project = hyline2.GetProject(location);
			XYZ project2 = verticalHyLine.GetProject(location);
			XYZ xyz = (new XYZ(location.X, location.Y, 0.0) - project2).Normalize();
			XYZ xyz2 = (new XYZ(location.X, location.Y, 0.0) - project).Normalize();
			try
			{
				double num = AssistFunc.mmToFeet(MarkPipeLevelUtility.fontHigtht * 0.5) * (double)view.Scale / 100.0;
				double num2 = AssistFunc.mmToFeet(MarkPipeLevelUtility.lineLenght) * (double)view.Scale / 100.0;
				double num3 = AssistFunc.mmToFeet(MarkPipeLevelUtility.fontHigtht * 0.4 * 2.0) * (double)view.Scale / 100.0;
				XYZ xyz3 = new XYZ();
				if ((int)view.DetailLevel == 3)
				{
					xyz3 = (hasLeadWire ? location : (project + xyz2 * movingDistance));
				}
				else
				{
					xyz3 = (hasLeadWire ? location : project);
				}
				XYZ xyz4 = xyz3 + num3 / 2.0 * xyz;
				XYZ endPoint = xyz3 + num3 * xyz;
				XYZ xyz5 = xyz4 + num * (xyz2 - xyz);
				XYZ xyz6 = xyz5 + 2.0 * num * xyz;
				XYZ endPoint2 = xyz6 + num2 * xyz;
				tagLocation = xyz6;
				if (hasLeadWire)
				{
					list.Add(YJKLineEx.YJKGetBound(pipePoint, xyz3));
				}
				list.Add(YJKLineEx.YJKGetBound(xyz3, endPoint));
				list.Add(YJKLineEx.YJKGetBound(xyz4, xyz5));
				list.Add(YJKLineEx.YJKGetBound(xyz4, xyz6));
				list.Add(YJKLineEx.YJKGetBound(xyz5, xyz6));
				list.Add(YJKLineEx.YJKGetBound(xyz6, endPoint2));
				MarkPipeLevelUtility.leaderLines.Add(YJKLineEx.YJKGetBound(xyz6, endPoint2));
				directionVector.HorizontalVector = xyz;
				directionVector.VerticalVector = xyz2;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		public static List<Line> DrawLeadWire(Autodesk.Revit.DB.View view, XYZ pipePoint, XYZ location, ref XYZ tagLocation, ref DirectionVector directionVector, YJKLine hyline)
		{
			List<Line> list = new List<Line>();
			YJKLine hyline2 = new YJKLine(new XYZ(hyline.StartPoint.X, hyline.StartPoint.Y, 0.0), new XYZ(hyline.EndPoint.X, hyline.EndPoint.Y, 0.0));
			hyline2.MakeUnBound();
			YJKLine verticalHyLine = MarkPipeLevelUtility.GetVerticalHyLine(hyline2, new XYZ(pipePoint.X, pipePoint.Y, 0.0));
			verticalHyLine.MakeUnBound();
			XYZ project = hyline2.GetProject(location);
			XYZ project2 = verticalHyLine.GetProject(location);
			XYZ xyz = (new XYZ(location.X, location.Y, 0.0) - project2).Normalize();
			XYZ verticalVector = (new XYZ(location.X, location.Y, 0.0) - project).Normalize();
			try
			{
				XYZ endPoint = location + xyz * (MarkPipeLevelUtility.lineLenght / 304.8) * (double)view.Scale / 100.0;
				list.Add(YJKLineEx.YJKGetBound(pipePoint, location));
				list.Add(YJKLineEx.YJKGetBound(location, endPoint));
				MarkPipeLevelUtility.leaderLines.Add(YJKLineEx.YJKGetBound(location, endPoint));
				tagLocation = location;
				directionVector.HorizontalVector = xyz;
				directionVector.VerticalVector = verticalVector;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		public static List<Line> DrawMutilSelectTriangle(Autodesk.Revit.DB.View view, XYZ pipePoint, XYZ location, ref XYZ tagLocation, ref DirectionVector directionVector, YJKLine hyline, XYZ mutilDirection)
		{
			List<Line> list = new List<Line>();
			YJKLine hyline2 = new YJKLine(new XYZ(hyline.StartPoint.X, hyline.StartPoint.Y, 0.0), new XYZ(hyline.EndPoint.X, hyline.EndPoint.Y, 0.0));
			hyline2.MakeUnBound();
			YJKLine verticalHyLine = MarkPipeLevelUtility.GetVerticalHyLine(hyline2, new XYZ(pipePoint.X, pipePoint.Y, 0.0));
			verticalHyLine.MakeUnBound();
			XYZ project = verticalHyLine.GetProject(location);
			XYZ xyz = (new XYZ(location.X, location.Y, 0.0) - project).Normalize();
			try
			{
				double num = AssistFunc.mmToFeet(MarkPipeLevelUtility.fontHigtht * 0.5) * (double)view.Scale / 100.0;
				double num2 = AssistFunc.mmToFeet(MarkPipeLevelUtility.lineLenght) * (double)view.Scale / 100.0;
				double num3 = AssistFunc.mmToFeet(MarkPipeLevelUtility.fontHigtht * 0.4 * 2.0) * (double)view.Scale / 100.0;
				XYZ xyz2 = pipePoint + num3 / 2.0 * xyz;
				XYZ endPoint = pipePoint + num3 * xyz;
				XYZ xyz3 = xyz2 + num * (mutilDirection - xyz);
				XYZ xyz4 = xyz3 + 2.0 * num * xyz;
				XYZ endPoint2 = xyz4 + num2 * xyz;
				tagLocation = xyz4;
				list.Add(YJKLineEx.YJKGetBound(pipePoint, endPoint));
				list.Add(YJKLineEx.YJKGetBound(xyz2, xyz3));
				list.Add(YJKLineEx.YJKGetBound(xyz2, xyz4));
				list.Add(YJKLineEx.YJKGetBound(xyz3, xyz4));
				list.Add(YJKLineEx.YJKGetBound(xyz4, endPoint2));
				MarkPipeLevelUtility.leaderLines.Add(YJKLineEx.YJKGetBound(xyz4, endPoint2));
				directionVector.HorizontalVector = xyz;
				directionVector.VerticalVector = mutilDirection;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		public static List<Line> DrawMutilSelectLeadWire(Autodesk.Revit.DB.View view, XYZ pipePoint, XYZ location, ref XYZ tagLocation, ref DirectionVector directionVector, YJKLine hyline, XYZ mutilDirection, List<Element> selectElems)
		{
			List<Line> list = new List<Line>();
			YJKLine hyline2 = new YJKLine(new XYZ(hyline.StartPoint.X, hyline.StartPoint.Y, 0.0), new XYZ(hyline.EndPoint.X, hyline.EndPoint.Y, 0.0));
			hyline2.MakeUnBound();
			YJKLine verticalHyLine = MarkPipeLevelUtility.GetVerticalHyLine(hyline2, new XYZ(pipePoint.X, pipePoint.Y, 0.0));
			verticalHyLine.MakeUnBound();
			XYZ project = verticalHyLine.GetProject(location);
			XYZ xyz = (new XYZ(location.X, location.Y, 0.0) - project).Normalize();
			try
			{
				XYZ xyz2 = new XYZ(pipePoint.X, pipePoint.Y, 0.0);
				double num = AssistFunc.mmToFeet(MarkPipeLevelUtility.lineLenght) * (double)view.Scale / 100.0;
				for (int i = 0; i < selectElems.Count; i++)
				{
					XYZ xyz3 = pipePoint + (double)i * mutilDirection * (AssistFunc.mmToFeet(MarkPipeLevelUtility.fontHigtht + MarkPipeLevelUtility.leadDistance * 2.0) * (double)view.Scale / 100.0);
					XYZ endPoint = xyz3 + num * xyz;
					list.Add(YJKLineEx.YJKGetBound(xyz3, endPoint));
					MarkPipeLevelUtility.leaderLines.Add(YJKLineEx.YJKGetBound(xyz3, endPoint));
				}
				if (selectElems.Count > 1)
				{
					list.Add(YJKLineEx.YJKGetBound(xyz2, xyz2 + (double)(selectElems.Count - 1) * mutilDirection * AssistFunc.mmToFeet(MarkPipeLevelUtility.fontHigtht + MarkPipeLevelUtility.leadDistance * 2.0) * (double)view.Scale / 100.0));
				}
				tagLocation = pipePoint;
				directionVector.HorizontalVector = xyz;
				directionVector.VerticalVector = mutilDirection;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		public static List<Line> DrawPlaneMultiSelectPipe(XYZ pipePoint, XYZ location)
		{
			List<Line> list = new List<Line>();
			try
			{
				list.Add(YJKLineEx.YJKGetBound(pipePoint, location));
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		public static YJKLine GetVerticalHyLine(YJKLine hyLine, XYZ point)
		{
			XYZ xyz = hyLine.Line.Direction.Normalize();
			XYZ xyz2 = new XYZ(-xyz.Y, xyz.X, xyz.Z).Normalize();
			return new YJKLine(point, point + xyz2);
		}

		public static double GetTansferDistance(double oriDistance, int factor = 30)
		{
			return oriDistance * (double)factor;
		}

		public static double fontHigtht = 1.0;

		public static double fontWidth = 1.0;

		public static double leadDistance = 0.0;

		public static double lineLenght = 900.0;

		public static List<Line> leaderLines = new List<Line>();
	}
}
